using Microsoft.AspNetCore.Mvc;
using Model.Utils;
using System;

namespace srb.net.Controllers.admin
{
    /// <summary>
    /// 简化实体生成测试控制器
    /// 用于测试简化实体生成功能
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class SimpleEntityGeneratorTestController : ControllerBase
    {
        /// <summary>
        /// 测试生成 integral_grade 和 borrower_attach 表的实体
        /// </summary>
        /// <returns>测试结果</returns>
        [HttpGet("TestGenerateCommonTables")]
        public IActionResult TestGenerateCommonTables()
        {
            try
            {
                // 测试数据库连接
                var connectionResult = SimpleEntityGenerator.TestConnection();
                if (!connectionResult.Success)
                {
                    return BadRequest(new
                    {
                        Success = false,
                        Message = "数据库连接失败",
                        Error = connectionResult.Error
                    });
                }

                // 生成 integral_grade 和 borrower_attach 表的实体
                var tables = new string[] { "integral_grade", "borrower_attach" };
                var result = SimpleEntityGenerator.GenerateEntities(tables, "Model");

                if (result.Success)
                {
                    return Ok(new
                    {
                        Success = true,
                        Message = "测试成功！已生成 integral_grade 和 borrower_attach 表的实体类",
                        Details = new
                        {
                            ConnectionTest = connectionResult,
                            GenerationResult = result,
                            GeneratedFiles = result.GeneratedFiles,
                            OutputPath = result.OutputPath,
                            SuccessTables = result.TableNames,
                            FailedTables = result.FailedTables
                        }
                    });
                }
                else
                {
                    return BadRequest(new
                    {
                        Success = false,
                        Message = "实体生成失败",
                        Error = result.Error,
                        Details = result
                    });
                }
            }
            catch (Exception ex)
            {
                return BadRequest(new
                {
                    Success = false,
                    Message = "测试过程中发生异常",
                    Error = ex.Message,
                    StackTrace = ex.StackTrace
                });
            }
        }
        
        /// <summary>
        /// 测试生成 dict 表的实体
        /// </summary>
        /// <returns>测试结果</returns>
        [HttpGet("TestGenerateDictTable")]
        public IActionResult TestGenerateDictTable()
        {
            try
            {
                // 测试数据库连接
                var connectionResult = SimpleEntityGenerator.TestConnection();
                if (!connectionResult.Success)
                {
                    return BadRequest(new
                    {
                        Success = false,
                        Message = "数据库连接失败",
                        Error = connectionResult.Error
                    });
                }

                // 生成 dict 表的实体
                var tables = new string[] { "dict" };
                var result = SimpleEntityGenerator.GenerateEntities(tables, "Model");

                if (result.Success)
                {
                    return Ok(new
                    {
                        Success = true,
                        Message = "测试成功！已生成 dict 表的实体类",
                        Details = new
                        {
                            ConnectionTest = connectionResult,
                            GenerationResult = result,
                            GeneratedFiles = result.GeneratedFiles,
                            OutputPath = result.OutputPath,
                            SuccessTables = result.TableNames,
                            FailedTables = result.FailedTables
                        }
                    });
                }
                else
                {
                    return BadRequest(new
                    {
                        Success = false,
                        Message = "实体生成失败",
                        Error = result.Error,
                        Details = result
                    });
                }
            }
            catch (Exception ex)
            {
                return BadRequest(new
                {
                    Success = false,
                    Message = "测试过程中发生异常",
                    Error = ex.Message,
                    StackTrace = ex.StackTrace
                });
            }
        }
        
        /// <summary>
        /// 测试生成单个表实体
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns>生成结果</returns>
        [HttpGet("TestGenerateSingleTable")]
        public IActionResult TestGenerateSingleTable(string tableName = "integral_grade")
        {
            try
            {
                var result = SimpleEntityGenerator.GenerateEntities(new[] { tableName }, "Model");
                
                if (result.Success)
                {
                    return Ok(new
                    {
                        Success = true,
                        Message = $"成功生成 {tableName} 表的实体类",
                        TableName = tableName,
                        GeneratedFile = result.GeneratedFiles?.Length > 0 ? result.GeneratedFiles[0] : "未知",
                        OutputPath = result.OutputPath
                    });
                }
                else
                {
                    return BadRequest(new
                    {
                        Success = false,
                        Message = $"生成 {tableName} 表实体失败",
                        Error = result.Error
                    });
                }
            }
            catch (Exception ex)
            {
                return BadRequest(new
                {
                    Success = false,
                    Message = "生成实体过程中发生异常",
                    Error = ex.Message
                });
            }
        }
    }
}
